package biz.bbtec.sbm.issuetracking.action;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.issuetracking.bean.AttachmentBean;
import biz.bbtec.sbm.issuetracking.bean.TaskBean;
import biz.bbtec.sbm.issuetracking.bean.WorkBean;
import biz.bbtec.sbm.issuetracking.factory.WorkPackageFactory;
import biz.bbtec.sbm.issuetracking.service.WorkPackageService;
import biz.bbtec.sbm.user.bean.UserBean;

public class WorkPackageAction extends ActionSupport implements SessionAware {

	private WorkBean work = new WorkBean();
	private List<WorkBean> worklist = new ArrayList<WorkBean>();
	private TaskBean task;
	private Map session;
	private AttachmentBean att = new AttachmentBean();

	private File image;
	private String imageFileName;
	private String imageFileContent;

	private UserBean user = new UserBean();
	private int userid;
	private List<UserBean> userlist = new ArrayList<UserBean>();
	private int worktaskid;
	private int pageNow;

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getWorktaskid() {
		return worktaskid;
	}

	public void setWorktaskid(int worktaskid) {
		this.worktaskid = worktaskid;
	}

	public List<UserBean> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<UserBean> userlist) {
		this.userlist = userlist;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	public AttachmentBean getAtt() {
		return att;
	}

	public void setAtt(AttachmentBean att) {
		this.att = att;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileContent() {
		return imageFileContent;
	}

	public void setImageFileContent(String imageFileContent) {
		this.imageFileContent = imageFileContent;
	}

	public List<WorkBean> getWorklist() {
		return worklist;
	}

	public void setWorklist(List<WorkBean> worklist) {
		this.worklist = worklist;
	}

	public TaskBean getTask() {
		return task;
	}

	public void setTask(TaskBean task) {
		this.task = task;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public WorkBean getWork() {
		return work;
	}

	public void setWork(WorkBean work) {
		this.work = work;
	}

	public String deleteWork() throws Exception {
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		List<AttachmentBean> attlist = workservice.attlist(work);
		if (attlist.size() != 0) {
			String path = ServletActionContext.getServletContext().getRealPath(
					"images");
			for (int i = 0; i < attlist.size(); i++) {
				File f = new File(path + "\\" + attlist.get(i).getTitle());
				if (f.exists()) {
					f.delete();
				}
			}
		}
		if (!workservice.DeleteWork(work)) {
			this.addActionError("未知错误！");
			return INPUT;
		} else {
			this.addActionError("删除成功！");
			return SUCCESS;
		}
	}

	public String modifyWork() throws Exception {
		if (work.getTaskid() == 0) {
			session = ActionContext.getContext().getSession();
			int temp = (Integer) session.get("worktaskid");
			work.setTaskid(temp);
		}

		if (image != null) {
			Random rnd = new Random();
			int p = rnd.nextInt(20);
			String num = Integer.toString(p);

			String path = ServletActionContext.getServletContext().getRealPath(
					"/images");

			imageFileName = num + "." + imageFileName;

			File file = new File(path + "//" + imageFileName);
			FileUtils.copyFile(image, file);
		} else {
			imageFileName = null;
		}

		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		user.setUserid(getUserid());
		if (!workservice.ModifyWork(work, user, imageFileName)) {
			this.addActionError("数据库操作失败！");

			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		session.remove("work");
		return SUCCESS;
	}

	public String addWork() throws Exception {
		if (image != null) {
			Random rnd = new Random();
			int p = rnd.nextInt(20);
			String num = Integer.toString(p);

			String path = ServletActionContext.getServletContext().getRealPath(
					"/images");

			imageFileName = num + "." + imageFileName;

			File file = new File(path + "//" + imageFileName);
			FileUtils.copyFile(image, file);
		} else {
			imageFileName = null;
		}

		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		user.setUserid(getUserid());
		if (user.getUserid() == -1) {
			this.addActionError("请选择工作负责人");
			return INPUT;
		}
		if (!workservice.AddWork(work, imageFileName, user)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		} else {
			this.addActionError("添加成功！");
			return SUCCESS;
		}
	}

	public String showallWork() throws Exception {
		if (work.getTaskid() != 0) {
			task = new TaskBean();
			task.setTaskid(work.getTaskid());
		}
		Map session = ActionContext.getContext().getSession();
		if (session.get("addworktaskid") != null) {
			task = new TaskBean();
			task.setTaskid((Integer) (session.get("addworktaskid")));
			session.remove("addworktaskid");
		}
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		worklist = workservice.ShowAllWork(task, getPageNow(), 15);
		if(getPageNow() == 0){
			int total = workservice.CountAllWork(task);
			int page = 0;
			do{
				total = total-15;
				page++;
			}while(total > 0);
			this.session.put("worktotalpage", page);
		}
		if (worklist.size() == 0) {
			this.addActionError("没有未完成工作！");
			this.session.put("worktaskid", task.getTaskid());
			return INPUT;
		} else {
			int worktaskid = worklist.get(0).getTaskid();
			this.session.put("worktaskid", worktaskid);
			return SUCCESS;
		}
	}

	public String showundoWork() throws Exception {
		if (work.getTaskid() != 0) {
			task = new TaskBean();
			task.setTaskid(work.getTaskid());
		}
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		worklist = workservice.ShowUndoWork(task);
		if (worklist.size() == 0) {
			this.addActionError("没有未完成工作！");
			this.session.put("worktaskid", task.getTaskid());
			return INPUT;
		} else {
			int worktaskid = worklist.get(0).getTaskid();
			this.session.put("worktaskid", worktaskid);
			return SUCCESS;
		}
	}

	public String showactivework() {
		if (work.getTaskid() != 0) {
			task = new TaskBean();
			task.setTaskid(work.getTaskid());
		}
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		worklist = workservice.ShowActiveWork(task);
		if (worklist.size() == 0) {
			this.addActionError("没有未完成工作！");
			this.session.put("worktaskid", task.getTaskid());
			return INPUT;
		} else {
			int worktaskid = worklist.get(0).getTaskid();
			this.session.put("worktaskid", worktaskid);
			return SUCCESS;
		}
	}

	public String showoneWork() throws Exception {

		if (work.getWpid() == 0) {
			Map session = ActionContext.getContext().getSession();
			int wpid = (Integer) session.get("attwpid");
			int taskid = (Integer) session.get("worktaskid");
			work.setWpid(wpid);
			work.setTaskid(taskid);
		}

		if (work.getTaskid() != 0) {
			task = new TaskBean();
			task.setTaskid(work.getTaskid());
		}

		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		work = workservice.ShowOneWork(work);
		if (work == null) {
			this.addActionError("出错啦！");
			this.session.put("attwpid", work.getWpid());
			return INPUT;
		} else {
			this.session.put("attwpid", work.getWpid());
			return SUCCESS;
		}
	}

	public String showallUser() throws Exception {

		if (task == null) {
			task = new TaskBean();
			Map session = ActionContext.getContext().getSession();
			int taskid = (Integer) session.get("worktaskid");
			task.setTaskid(taskid);
		}

		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		userlist = workservice.ShowAllUser(task);
		this.session.put("addworktaskid", task.getTaskid());
		return SUCCESS;
	}

	public String showMain() {
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		worklist = workservice.showmain();
		if (worklist.size() == 0) {
			this.addActionError("没有逾期工作！");
			return INPUT;
		} else {
			return SUCCESS;
		}
	}
	
	public String AlertWork() throws SQLException{
		WorkPackageService workservice = WorkPackageFactory.getInstance()
				.creatWorkPackageService();
		if(workservice.AlertWork(work)){
			return SUCCESS;
		}
		
		else  
			return INPUT;
	}

}
